package com.test.controller;

import java.io.IOException;
import java.sql.Blob;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.plaf.metal.MetalDesktopIconUI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.dao.MailDao;
import com.test.model.Mail;
@Controller
@RequestMapping("/mail")
@EnableScheduling
public class MailController {
	private final Log logger = LogFactory.getLog(MailController.class);
	@Resource
	private MailDao mailDao;
	/*public static void main(String[] args) throws MessagingException, IOException {
		Properties props = new Properties();
		props.setProperty("mail.store.protocol", "imap"); 
        props.setProperty("mail.imap.host", "imap.126.com"); 
        props.setProperty("mail.imap.port", "143");
     // 创建Session实例对象 
        Session session = Session.getInstance(props); 
         
        // 创建IMAP协议的Store对象 
        Store store = session.getStore("imap"); 
         
        // 连接邮件服务器 
       store.connect("sftwangbo@126.com", "wb961109"); 
        
        // 获得收件箱 
        Folder folder = store.getFolder("INBOX"); 
        // 以读写模式打开收件箱 
        folder.open(Folder.READ_WRITE); 
         
        // 获得收件箱的邮件列表 
        Message[] messages = folder.getMessages();
        System.out.println(messages.length);
        for(Message message:messages){
        	String filename = message.getFileName();
        	//System.out.println(filename);
        	//System.out.println(message.getFrom()[0]);
        	System.out.println(message.getHeader("from")[0]) 
        	System.out.println(message.getReceivedDate());
        }
	}*/
	
	private static Properties props;
	//private static Session session;
	private static Folder folder;
	
	static{
		 props = new Properties();
		props.setProperty("mail.store.protocol", "imap"); 
        props.setProperty("mail.imap.host", "imap.126.com"); 
        props.setProperty("mail.imap.port", "143");
     
       try {
    	// 创建Session实例对象 
           Session session = Session.getInstance(props); 
            
           // 创建IMAP协议的Store对象 
           Store store = session.getStore("imap"); 
            
           // 连接邮件服务器 
		store.connect("sftwangbo@126.com", "wb961109");
		 // 获得收件箱 
         folder = store.getFolder("INBOX"); 
      // 以读写模式打开收件箱 
         folder.open(Folder.READ_WRITE); 
	} catch (MessagingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
        
       
	}
	@Scheduled(cron="0 0 6 * * ?")
	public void reciveMail(){
		 // 获得收件箱的邮件列表 
       try {
    	   Message[] messages = folder.getMessages();
           System.out.println(messages.length);
           logger.info("收取邮件开始》》》》》》》》");
           for(Message message:messages){
           	String filename = message.getFileName();
           	//System.out.println(filename);
           	//System.out.println(message.getFrom()[0]);
           	//System.out.println(message.getHeader("from")[0]);
          // 	System.out.println(message.getReceivedDate());
        	  Mail mail = new Mail();
        	  mail.setId(UUID.randomUUID().toString().replace("-",""));
        	  mail.setFrom(message.getFrom()[0].toString());
        	  mail.setContent(message.getContent().toString());
        	  mail.setRecdate(message.getReceivedDate());
        	  mail.setSubject(message.getSubject());
        	  mail.setUser("系统定时收取");
        	//  logger.info(mail);
        	  mailDao.insert(mail);
        	  message.setFlag(Flag.DELETED,true);
        	 
           	
           }
           logger.info("收取邮件结束》》》》共收取："+messages.length+"封邮件");
	} catch (Exception e) {
		// TODO: handle exception
	}
	}
	
	@RequestMapping("/getMail")
	@ResponseBody
	public Object getAllMail(){
		List<Map<String, Object>> mails = mailDao.getAllMail();
		return mails;
	}
	@RequestMapping("/mailview")
	public String mailView(Model model){
		List<Map<String, Object>> mails = mailDao.getAllMail();
		model.addAttribute("mails",mails);
		return "mail/mailList";
	}
}

